#解析数据内容
from lxml import etree
import json
from openpyxl import Workbook
import requests
import pandas as pd

heeaders={
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3875.400 QQBrowser/10.8.4492.400'
}
response=requests.get(url='https://voice.baidu.com/act/newpneumonia/newpneumonia/?from=osari_aladin_banner#tab4',headers=heeaders).text

html=etree.HTML(response)
#在网页中寻找我们想要的数据
result=html.xpath('//*[@id="captain-config"]/text()')

result=json.loads(result[0])
#print(result)
#一层一层找自己所用到的数据
result_out=result["component"][0]
#print(result_out)

#获取国内疫情数据
result_in=result["component"][0]["caseList"]
print(result_in)



#循环遍历数据，按照位置顺序
ws=[]
for each in result_in:
    temp_list=[each['area'],each['confirmedRelative'],each['confirmed'],each['crued'],each['died']]
    ws.append(temp_list)

name=['省份','新增','累计','治愈','死亡',]
text=pd.DataFrame(columns=name,data=ws)
text.to_csv('./data_text.csv')